package com.ruoyi.service.impl;

import com.ruoyi.utils.TreeNode;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.CommentMapper;
import com.ruoyi.service.ICommentService;

/**
 * 评论Service业务层处理
 *
 * @author qyb
 * @date 2023-12-26
 */
@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ISysUserService sysUserService;


    @Override
    public AjaxResult comment(Long id) {

        TreeNode<String> treeRoot = getSetA();
        for (TreeNode<String> node : treeRoot) {
            String indent = createIndent(node.getLevel());
            System.out.println(indent + node.data);
        }
//        List<Comment> comments = commentMapper.selectComment(id);
//
//        //所有CommentVo对象的列表
//        List<CommentVo> commentVos=new ArrayList<>();
//        //补充评论内容 copy属性转换为vo
//
//        for (Comment comment : comments) {
//            CommentVo commentVo=new CommentVo();
//            BeanUtils.copyProperties(comment,commentVo);
//            //作者信息
//            SysUser author = sysUserService.getById(comment.getUserId());
//            SysUser authorVo=new SysUser();
//            BeanUtils.copyProperties(author,authorVo);
//            commentVo.setUser(authorVo);
//            //toUser信息 level>1 说明有回复对象
////            if(comment.getLevel()>1){
////                SysUser toUserVo=new SysUser();
////                SysUser toUser=sysUserService.getById(comment.getToUid());
////                BeanUtils.copyProperties(toUser,toUserVo);
////                commentVo.setToUser(toUserVo);
////            }
//            commentVos.add(commentVo);
//        }
//        //构造成树
//        TreeNode myTree = TreeNodeIterator.buildTree(commentVos);
//        //根节点的子节点（即level为1的评论 ）
//        List<TreeNode> children = myTree.getChildren();
//        List<CommentVo> level1CommentVo=new ArrayList<>();
//        for (TreeNode child : children) {
//            level1CommentVo.add(child.getContent());
//        }
//        return AjaxResult.success(level1CommentVo);
//    }
//
//    @Override
//    public List<Comment> comments(Long id) {
//
//        List<Comment> comments = commentMapper.select(id);
//        return comments;
//    }
//
//    public AjaxResult getComments(Long id){
//        List<Comment> comments = commentMapper.selectComment(id);
//        ArrayList<CommentVo> returnComments = new ArrayList<>();
//        for (Comment comment:comments
//             ) {
//            //找父节点
//            CommentVo commentVo = new CommentVo();
//            if (comment.getParentId()==0){
//                BeanUtils.copyProperties(comment,commentVo);
//                returnComments.add(commentVo);
//            }
//        }
//        return null;
//    }
        return null;

    }



    public static TreeNode<String> getSetA() {

        TreeNode<String> A = new TreeNode<String>("A");
        {
            TreeNode<String> B = A.addChild("B");
            TreeNode<String> C = A.addChild("C");
            TreeNode<String> D = A.addChild("D");
            {
                TreeNode<String> E = B.addChild("E");
                TreeNode<String> F = C.addChild("F");
                TreeNode<String> G = C.addChild("G");
                {
                    TreeNode<String> H = F.addChild("H");
                    TreeNode<String> I = F.addChild("I");
                    TreeNode<String> J = F.addChild("J");
                }
            }
        }

        return A;
    }

    private static String createIndent(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append(' ');
        }
        return sb.toString();
    }
}
